package com.wf.smartcomm.server.serviceImpl;

import java.math.BigInteger;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Complain;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.ComplainService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午3:06:20 
 * 
 */
@Service
@Transactional
public class ComplainServiceImpl implements ComplainService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Complain complain) {
		complain.setObjectId(IdUtils.uuid());
		complain.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(complain);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Complain where user = :user");
		query.setParameter("user", user);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Complain comp where comp.user = :user order by comp.createdAt desc");
		query.setParameter("user", user);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	   @Override
	   @Transactional(propagation=Propagation.NOT_SUPPORTED)
	   public void list(Village village, Page page)
	   {
	     Query query = this.factory.getCurrentSession().createSQLQuery("select count(*) from t_complain where uid in (select objectId from t_user where vid =:vid)");
	     query.setParameter("vid", village.getObjectId());
	     BigInteger counts = (BigInteger)query.uniqueResult();
	     page.setTotalCount(counts.intValue());
	     query = this.factory.getCurrentSession().createSQLQuery("select * from t_complain where uid in (select objectId from t_user where vid =:vid) order by createdAt desc").addEntity(Complain.class);
	     query.setParameter("vid", village.getObjectId());
	     query.setMaxResults(page.getPageSize());
	     query.setFirstResult(page.getBeginIndex());
	     page.setDataList(query.list());
	   }
 
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Complain getById(String objectId) {
		Query query = factory.getCurrentSession().createQuery("from Complain where objectId = :id");
		query.setParameter("id", objectId);
		return (Complain) query.uniqueResult();
	}

	@Override
	public void update(Complain complain) {
		Query query =factory.getCurrentSession().createQuery("update Complain set status = :status where objectId = :id");
		query.setParameter("status", ConstantUtils.COMPLAIN_STATUS_PROCESSING);
		query.setParameter("id", complain.getObjectId());
		query.executeUpdate();
	}
/*     */   @Transactional(propagation=Propagation.NOT_SUPPORTED)
/*     */   public void listByKey(Village village, String sSearch, Page page)
/*     */   {
/*  88 */     String hql = "select count(*) from t_complain where uid in (select objectId from t_user where vid =:vid) and content like :key";
/*  89 */     Query query = this.factory.getCurrentSession().createSQLQuery(hql);
/*  90 */     query.setParameter("vid", village.getObjectId());
/*  91 */     query.setParameter("key", "%" + sSearch + "%");
/*  92 */     BigInteger counts = (BigInteger)query.uniqueResult();
/*  93 */     page.setTotalCount(counts.intValue());
/*  94 */     hql = "select * from t_complain where uid in (select objectId from t_user where vid =:vid) and content like :key order by createdAt desc";
/*  95 */     query = this.factory.getCurrentSession().createSQLQuery(hql).addEntity(Complain.class);
/*  96 */     query.setParameter("vid", village.getObjectId());
/*  97 */     query.setParameter("key", "%" + sSearch + "%");
/*  98 */     query.setFirstResult(page.getBeginIndex());
/*  99 */     query.setMaxResults(page.getPageSize());
/* 100 */     page.setDataList(query.list());
/*     */   }
/*     */ }
